package logic

import (
	"context"

	"order_rpc/internal/svc"
	"order_rpc/pb/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetOrderByIDLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetOrderByIDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderByIDLogic {
	return &GetOrderByIDLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetOrderByIDLogic) GetOrderByID(in *order.IDRequest) (*order.OrderInfo, error) {
	orderInfo, err := l.svcCtx.OrderModel.FindOneById(l.ctx, in.OrderId)
	if err != nil {
		return nil, err
	}
	goodList, err := l.svcCtx.OrderGoodsModel.FindListByOrderIDList(l.ctx, []int64{in.OrderId})
	if err != nil {
		return nil, err
	}

	var goods []*order.GoodInfo
	for _, good := range goodList {
		goods = append(goods, &order.GoodInfo{
			GoodId:    good.GoodId,
			GoodName:  good.GoodName,
			GoodPrice: good.GoodPrice,
			GoodCount: good.GoodCount,
		})
	}
	return &order.OrderInfo{
		UserId:      orderInfo.UserId,
		OrderId:     orderInfo.OrderId,
		OrderTotal:  orderInfo.OrderTotal,
		OrderStatus: orderInfo.OrderStatus,
		CreateTime:  orderInfo.CreateTime,
		UpdateTime:  orderInfo.UpdateTime,
		DeleteTime:  orderInfo.DeleteTime,
		Goods:       goods,
	}, nil
}
